#解释层 对于业务数据进行解释和执行

from glab import *
from __init__ import log


#保存每个模块ID的在hass中的位置信息,当模块主动发送状态时候，更改UI上的状态
ID2id = {}
def exp(s = " "):
    log("")
    a = s.split(' ')

    #04 08 88 21 2B 03 C5
    #调光模块
    if a[1] == "08" and a[2]=="88":
        dom_id = ID2id.get(a[0] + iif(a[3]=="11","0","1"),"None")
        if(dom_id!="None"):
            lights[dom_id]._state=iif(dec(a[4])>0,True,False)
            if a[4] != "00":
                lights[dom_id]._brightness = dec(a[4])*2.55
            
            lights[dom_id].schedule_update_ha_state()

        return " " + a[0] + "亮度反馈:" + iif(a[3] == "11", "A", "B") + "亮度=" + str(dec(a[4])) + " 加速度=" + a[5] + " index" + str(dom_id)

    #灯光模块
    if a[3] == "02":
        #['00', '03', '00', '02', 'FD', '00', 'A5', '4B']
        #
        dom_id = ID2id.get(a[0] + a[5][-1],"None")
        log("dom_id %d %s a[5][-1] %s",dom_id,a,a[5][-1])
        #继电器 1=00 03 00 02 FD 05 65 
        if a[4] == "FD" or a[4] == "FC":
            if dom_id != "None":
                lights[dom_id]._state = iif(a[4]=="FD",True,False)
                lights[dom_id].schedule_update_ha_state()
            return " " + a[0] + "开关设定:" + iif(a[4] == "FD","开","关") + " index" + str(dom_id)
        #拉菲面板
        else:
            return " " + a[0] + "面板按下"

    return "未知"